package com.cj.bookstore.pay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.ExtendParams;
import com.alipay.api.domain.TbapiQueryAmountBizContent;
import com.alipay.api.request.AlipayOpenPublicTemplateMessageIndustryModifyRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayOpenPublicTemplateMessageIndustryModifyResponse;
import com.alipay.api.response.AlipayTradeCloseResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.cj.bookstore.common.utils.HttpClientUtils;
import com.cj.bookstore.model.entity.BookInfo;
import com.cj.bookstore.model.entity.Orders;
import org.apache.http.client.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Service
public class Alipay extends AbstractPay{

    @Autowired
    private AlipayConfig alipayConfig;
    private final static  String APP_ID = "2021000118671353";
    private final static String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDFgoa/Y3HKvKNuwlKdQUR2dJcXurZNFHg8K7JeDg6TPdI1QNPlN7cKvgDe82EfUZu0hNiNVq9JXa28us0oREfeIuz0dPzLGmrRNWoO34f/2H47h2Ql5lXxrqyoXt+UT+hPO8hHnsGOWrFXo0Dhr0rKBdLIEFUiIlzeAQABMxYoSEj8cSTWkzKdCvWdDWU0VcXiiD2+BHWxMLsHGm8dZHTlAxC/J0Yu7HpY8vME1TsI9gy5MrEx6kjXquNIm7I76jDzYmYDQpj9o1bLwGXwYQ+VIn2yL1UF45wBChlv3ihhJYApZfAHQ6hAe9+o/Y9H5mopvw31pJcT/hk5xrWEHGkFAgMBAAECggEAbUiJkkBN6CV0MIao+4XvgbOFHoYa9I9M784lxVD105azRu/1zrCDQRhclwAHTzLyenG++IV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eldYJ2p/RYpQkhrt+qY5EeFegQALfOqTWg0wm5cZ7E+h/i7xA8dkWrkkx8=";
    private final static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApUYIPGt8tOff1lRvrN4n2ztzik4zT5sup3m0BWo9PLkmDES95CN9Jm5O0ipv3847dwkXdE9n0jj3+T/Vc/Sr6dFNsMebnOJ71YG28TUOylngjtZswhnO6UDKRGkSgAcQBHpV9g78MMbYXDFiecUKiOyTlAYHpzVjtR2sihIbuuGH93krI88lOrT7rdikASKKplvrUwnMFEiIfzqfYSQf5x7xBApYpk9R2MzkKrx7ZA8TJ433grsNr+56lyCWhimUplfzVOkHpHqXkib3DaGIkNHUo8g+oQBwTrZ2jFo6OnbopRAVhZYEN0RYCGPDKap6kDbT87vROtUSKAdWTI3R8QIDAQAB";
    private final static String CHARSET = "UTF-8";
    @Override
    public void prepareAndPay(PayContext payContext) throws IOException, AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",APP_ID,APP_PRIVATE_KEY,"json",CHARSET,ALIPAY_PUBLIC_KEY,"RSA2"); //获得初始化的AlipayClient
        Orders order = payContext.getOrders();
        BookInfo book = payContext.getBookInfos().get(0);
        HttpServletResponse httpResponse = payContext.getResponse();
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.open.public.template.message.industry.modify
        AlipayTradePagePayRequest request =  new  AlipayTradePagePayRequest();
        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
        request.setReturnUrl( "http://localhost:8081/payment/return" );
        request.setNotifyUrl( "http://localhost:8081/payment/return" ); //在公共参数中设置回跳和通知地址
        request.setBizModel(model);
        model.setOutTradeNo(order.getOrderId());
        model.setTotalAmount(order.getPayment());
        model.setSubject(book.getOutline());
        model.setBody(book.getOutline());
        model.setPassbackParams("merchantBizType%3d3C%26merchantBizNo%3d2016010101111");
        ExtendParams extendParams = new ExtendParams();
        model.setExtendParams(extendParams);
        extendParams.setSysServiceProviderId("2088621957315964");
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        AlipayTradePrecreateResponse response = null;

        String form= "" ;
        try  {
            form = alipayClient.pageExecute(request).getBody();  //调用SDK生成表单
        }  catch  (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType( "text/html;charset="  + CHARSET);
        httpResponse.getWriter().write(form); //直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }


    @Override
    public void afterPay(PayContext payContext) {
    }
}
